package com.example.datn.activities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.example.datn.R;
import com.example.datn.adapter.VideoItemAdapter;
import com.example.datn.manager.ResourceManager;
import com.example.datn.manager.SingerManager;
import com.example.datn.manager.SongManager;
import com.example.datn.manager.UrlManager;
import com.example.datn.manager.VideoManager;
import com.example.datn.objects.SingerOject;
import com.example.datn.objects.SongObject;
import com.example.datn.objects.VideoObject;
import com.example.datn.transformer.Transformer;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class ViewListVideoActivity extends Activity implements OnItemClickListener{

	ListView lv_listVideo;
	ArrayList<VideoObject> listVideo;
	VideoItemAdapter adapter;
	TextView tv_singerName, tv_status_connection;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		
		SingerOject singer = SingerManager.getInstance().getCurrentSinger();
		
		setContentView(R.layout.list_videos);
		initViews();
				
		if(!ResourceManager.isNetworkConnected(getApplicationContext()))
			tv_status_connection.setVisibility(View.VISIBLE);
		else
			tv_status_connection.setVisibility(View.GONE);
		
		listVideo = VideoManager.getInstance().getListVideo();
		if(listVideo == null || listVideo.size() == 0)
			new loadListVideo().execute(UrlManager.linkGetVideo+singer.singerId);
		else{
			adapter = new VideoItemAdapter(this, R.layout.list_videos, listVideo);
			lv_listVideo.setAdapter(adapter);
			lv_listVideo.setOnItemClickListener(this);
		}
		
		tv_singerName.setText(singer.singerName);
		
		//giai phong bai hat dang chay ngam
		MediaPlayer media = SongManager.getInstance().getCurrentMediaPlayer();
		try{
			if(media != null)
				//media.reset();
				SongManager.killMediaPlayer(media);
		}catch(Exception e){
			Log.d("----loi kill", ": " +e.toString());
		}
	}	

	private void initViews() {
		lv_listVideo = (ListView)findViewById(R.id.lv_list_video);
		tv_singerName = (TextView)findViewById(R.id.tv_singerName_video);
		tv_status_connection = (TextView)findViewById(R.id.tv_status_connect_listsong);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		VideoObject video = VideoManager.getInstance().getListVideo().get(position);
		VideoManager.getInstance().setCurrentVideo(video);
		Intent i = new Intent(this, PlayVideoActivity.class);
		startActivity(i);
	}

	
	private class loadListVideo extends AsyncTask<String, Void, Void>{
		private String Content;
    	private String Error = null;      	
    	ArrayList<VideoObject> listVideo;// = new ArrayList<SongObject>();
    	
		@Override
		protected Void doInBackground(String... params) {
			BufferedReader reader=null;              
            // Send data 
			try {
				// Defined URL  where to send data
				URL url = new URL(params[0]); 
				 // Send POST data request
				URLConnection connection = url.openConnection();					
				connection.setConnectTimeout(5000); //set timeout to 5 seconds								
				
				reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				StringBuilder sb = new StringBuilder();
				
				String line = null;
                while((line = reader.readLine()) != null){
                    sb.append(line + " ");
               }
               Content = sb.toString();
				
			} catch (MalformedURLException e) {
				Error = e.toString();
				e.printStackTrace();
			} catch (Exception e) {
				Error = e.toString();
				e.printStackTrace();
			}
			finally{
				try{      
                    reader.close();
                }    
                catch(Exception ex) {}
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {			
			super.onPostExecute(result);
			
			if(Error == null){
				listVideo = Transformer.TransformToListVideo(Content);
				VideoManager.getInstance().setListVideo(listVideo);
				
				adapter = new VideoItemAdapter(ViewListVideoActivity.this, R.layout.list_videos, listVideo);
				lv_listVideo.setAdapter(adapter);
				lv_listVideo.setOnItemClickListener(ViewListVideoActivity.this);
			}
		}
		
		
	}
	
}
